﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;

namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// socketTest.xaml 的交互逻辑
    /// </summary>
    public partial class socketTest : Window
    {
        IPEndPoint pEndPoint;
        Socket client;

        public socketTest()
        {
            InitializeComponent();
            //initSocket();
        }

        private void initSocket()
        {
            if (pEndPoint == null)
            {
                //pEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8888);
                pEndPoint = new IPEndPoint(IPAddress.Parse(txt_IP.Text), int.Parse(txt_PORT.Text));
            }
            if (client == null) 
            {
                client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            }
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void btnConect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                initSocket();
                client.Connect(pEndPoint);
                MessageBox.Show("连接成功!");
            }
            catch (Exception ex)
            {

                MessageBox.Show("链接出错："+ex.Message.ToString());
            }
            
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string msg = this.sendMsg.Text;
                byte[] bytes = Encoding.ASCII.GetBytes(msg);
                client.Send(bytes);
                //接收响应数据
                byte[]responseBytes = new byte[1024];
                int bytesRec = client.Receive(responseBytes);
                string response =Encoding.ASCII.GetString(responseBytes,0,bytesRec);
                RecivedMsg.Text = response;
                
            }
            catch (Exception ex)
            {

                MessageBox.Show("发送出错"+ex.Message.ToString());
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            client.Shutdown(SocketShutdown.Both);
            client.Close();
            client.Dispose();
            pEndPoint = null;
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void btnReconect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                initSocket();
                client.Connect(pEndPoint);
                MessageBox.Show("连接成功!");
            }
            catch (Exception ex)
            {

                MessageBox.Show("链接出错：" + ex.Message.ToString());
            }
        }
    }
}
